import Mock from 'mockjs'

const Random = Mock.Random

// ai生成: 内存持久化数据
let userList = [
  { userId: 1, username: 'admin', password: 'admin', phone: '13800000000', role: 'ADMIN', nickname: '管理员', avatar: '/src/assets/default-avatar.png' },
  { userId: 2, username: 'user', password: 'user123', phone: '13900000000', role: 'USER', nickname: '测试用户', avatar: '/src/assets/default-avatar.png' }
]
let orderList = [] // ai生成
let reviewList = [] // ai生成
let userIdSeq = 3 // ai生成
let orderIdSeq = 1 // ai生成
let reviewIdSeq = 1 // ai生成

Random.extend({
  country: function() {
    const countries = ['中国', '美国', '日本', '韩国', '英国', '法国', '德国', '意大利', '俄罗斯', '加拿大']
    return this.pick(countries)
  }
})

// 生成固定的国家和城市数据
const countryList = Array.from({ length: 20 }).map((_, i) => ({
  countryId: i + 1,
  countryName: Random.country(),
  countryIsoCode: Random.word(2).toUpperCase(),
  continentId: Random.integer(1, 7)
}))

const cityList = Array.from({ length: 20 }).map((_, i) => ({
  cityId: i + 1,
  cityName: Random.city(),
  state: Random.province(),
  countryId: Random.integer(1, 20)
}))

// 固定的酒店测试数据
const hotelList = [
  {
    hotelId: 1,
    hotelName: '北京国际大酒店',
    chain_id: 1,
    brand_id: 1,
    cityId: 1,
    countryId: 1,
    addressline1: '北京市朝阳区建国门外大街1号',
    starRating: 5,
    longitude: 116.433, 
    latitude: 39.913
  },
  {
    hotelId: 2,
    hotelName: '上海外滩假日酒店',
    chain_id: 2,
    brand_id: 2,
    cityId: 2,
    countryId: 1,
    addressline1: '上海市黄浦区中山东一路2号',
    starRating: 4,
    longitude: 121.490,
    latitude: 31.241
  },
  {
    hotelId: 3,
    hotelName: '广州白云宾馆',
    chain_id: 1,
    brand_id: 1,
    cityId: 3,
    countryId: 1,
    addressline1: '广州市白云区机场路1号',
    starRating: 4,
    longitude: 113.298,
    latitude: 23.387
  }
]

// mock 房型数据
const roomList = [
  {
    roomId: 1,
    hotelId: 1,
    name: '温馨大床房',
    description: '35平米 | 大床 | 山景',
    price: 342,
    image: '/room1.jpg',
    tags: ['免费WiFi', '窗户'],
    available: 5
  },
  {
    roomId: 2,
    hotelId: 1,
    name: '豪华双床房',
    description: '40平米 | 双床 | 城市景观',
    price: 388,
    image: '/room2.jpg',
    tags: ['免费WiFi', '浴缸'],
    available: 3
  },
  {
    roomId: 3,
    hotelId: 2,
    name: '商务套房',
    description: '50平米 | 大床 | 江景',
    price: 520,
    image: '/room3.jpg',
    tags: ['免费WiFi', '早餐'],
    available: 2
  }
]

// ai生成: 生成 dailyPrices 字段辅助函数
function getDateRange(start, end) { // ai生成
  const arr = [];
  let dt = new Date(start);
  const endDt = new Date(end);
  while (dt < endDt) {
    arr.push(dt.toISOString().split('T')[0]);
    dt.setDate(dt.getDate() + 1);
  }
  return arr;
}

export default [
  {
    url: '/admin/resources/countries',
    method: 'get',
    statusCode: 200,
    response: () => ({
      code: 200,
      data: countryList
    })
  },
  {
    url: '/admin/resources/countries',
    method: 'post',
    statusCode: 200,
    response: ({ body }) => ({
      code: 200,
      data: {
        countryId: Random.integer(100, 999),
        ...body
      },
      message: '添加成功'
    })
  },
  {
    url: '/admin/resources/cities',
    method: 'get',
    statusCode: 200,
    response: () => ({
      code: 200,
      data: cityList
    })
  },
  {
    url: '/admin/resources/cities',
    method: 'post',
    statusCode: 200,
    response: ({ body }) => ({
      code: 200,
      data: {
        cityId: Random.integer(100, 999),
        ...body
      },
      message: '添加成功'
    })
  },
  {
    url: '/admin/resources/hotels',
    method: 'get',
    statusCode: 200,
    response: () => ({
      code: 200,
      data: hotelList
    })
  },
  {
    url: '/admin/resources/hotels',
    method: 'post',
    statusCode: 200,
    response: ({ body }) => {
      const newHotel = {
        hotelId: Random.integer(100, 999),
        ...body
      }
      hotelList.push(newHotel)
      return {
        code: 200,
        data: newHotel,
        message: '添加成功'
      }
    }
  },
  {
    url: /\/admin\/resources\/hotels\/\d+$/,
    method: 'delete',
    statusCode: 200,
    response: (options) => {
      const url = options.url || (options.req && options.req.url)
      const match = url && url.match(/\/hotels\/(\d+)$/)
      const hotelId = match ? match[1] : null
      const idx = hotelList.findIndex(h => String(h.hotelId) === String(hotelId))
      if (idx !== -1) hotelList.splice(idx, 1)
      return {
        code: 200,
        message: '删除成功'
      }
    }
  },
  {
    url: '/admin/resources/rooms',
    method: 'get',
    statusCode: 200,
    response: ({ query }) => {
      const hotelId = query && query.hotelId
      const filtered = hotelId ? roomList.filter(r => String(r.hotelId) === String(hotelId)) : roomList
      return {
        code: 200,
        data: filtered
      }
    }
  },
  // ai生成: 用户注册接口
  {
    url: '/api/users/register',
    method: 'post',
    statusCode: 200,
    response: ({ body }) => {
      const exist = userList.find(u => u.username === body.username)
      if (exist) return { code: 400, message: '用户名已存在' }
      const user = { ...body, userId: userIdSeq++, role: 'USER', avatar: '/src/assets/default-avatar.png' }
      userList.push(user)
      return { code: 200, data: user }
    }
  },
  // ai生成: 用户登录接口
  {
    url: '/api/users/login',
    method: 'post',
    statusCode: 200,
    response: ({ query }) => {
      const { username, password } = query
      const user = userList.find(u => u.username === username && u.password === password)
      if (!user) return { code: 401, message: '用户名或密码错误' }
      return { code: 200, data: { token: 'mock-token-' + Date.now(), userInfo: user } }
    }
  },
  // ai生成: 获取用户信息
  {
    url: /\/api\/users\/(\d+)$/,
    method: 'get',
    statusCode: 200,
    response: ({ url }) => {
      const match = url.match(/\/users\/(\d+)$/)
      const userId = match ? Number(match[1]) : null
      const user = userList.find(u => u.userId === userId)
      if (!user) return { code: 404, message: '用户不存在' }
      return { code: 200, data: user }
    }
  },
  // ai生成: 创建订单
  {
    url: '/api/order/orders/create',
    method: 'post',
    statusCode: 200,
    response: ({ body }) => {
      // ai生成: 自动生成 dailyPrices 字段和总价
      const dailyPrices = {};
      const dates = getDateRange(body.checkIn, body.checkOut); // ai生成
      dates.forEach(date => {
        dailyPrices[date] = 342; // ai生成，实际可根据房型定价
      });
      const order = { ...body, id: orderIdSeq++, status: 'ongoing', statusText: '进行中', createTime: new Date().toISOString(), totalPrice: dates.length * 342, dailyPrices }; // ai生成
      orderList.push(order);
      return { code: 200, data: order };
    }
  },
  // ai生成: 取消订单
  {
    url: '/api/order/orders/cancel',
    method: 'post',
    statusCode: 200,
    response: ({ body }) => {
      const idx = orderList.findIndex(o => o.id === body.orderId)
      if (idx !== -1) orderList[idx].status = '已取消'
      return { code: 200, message: '订单已取消' }
    }
  },
  // ai生成: 获取订单详情
  {
    url: /\/admin\/order\/(\d+)$/,
    method: 'get',
    statusCode: 200,
    response: ({ url }) => {
      const match = url.match(/\/order\/(\d+)$/)
      const orderId = match ? Number(match[1]) : null
      const order = orderList.find(o => o.id === orderId)
      if (!order) return { code: 404, message: '订单不存在' }
      return { code: 200, data: order }
    }
  },
  // ai生成: 获取用户订单列表
  {
    url: /\/admin\/order\/user\/(\d+)$/,
    method: 'get',
    statusCode: 200,
    response: ({ url }) => {
      const match = url.match(/\/user\/(\d+)$/)
      const userId = match ? Number(match[1]) : null
      const orders = orderList.filter(o => o.guestId === userId)
      return { code: 200, data: orders }
    }
  },
  // ai生成: 添加评论
  {
    url: '/api/review',
    method: 'post',
    statusCode: 200,
    response: ({ body }) => {
      const review = { ...body, reviewId: reviewIdSeq++ }
      reviewList.push(review)
      return { code: 200, data: review }
    }
  },
  // ai生成: 获取酒店评论
  {
    url: /\/api\/review\/hotel\/(\d+)$/,
    method: 'get',
    statusCode: 200,
    response: ({ url }) => {
      const match = url.match(/\/hotel\/(\d+)$/)
      const hotelId = match ? Number(match[1]) : null
      const reviews = reviewList.filter(r => r.hotelId === hotelId)
      return { code: 200, data: reviews }
    }
  }
] 